package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.onlineedubackend.dto.ForumUserStatsDTO;
import org.csu.onlineedubackend.entity.ForumUserStats;
import org.csu.onlineedubackend.entity.User;
import org.csu.onlineedubackend.mapper.ForumUserStatsMapper;
import org.csu.onlineedubackend.mapper.UserMapper;
import org.csu.onlineedubackend.service.ForumUserStatsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 論壇用戶統計服務實現類
 */
@Service
public class ForumUserStatsServiceImpl implements ForumUserStatsService {

    @Autowired
    private ForumUserStatsMapper forumUserStatsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean updateUserStats(Integer userId) {
        // 檢查用戶是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }
        
        // 檢查統計記錄是否存在
        ForumUserStats stats = forumUserStatsMapper.selectById(userId);
        if (stats == null) {
            // 創建新的統計記錄
            stats = new ForumUserStats();
            stats.setUserId(userId);
            stats.setForumPostCount(0);
            stats.setForumLikeCount(0);
            stats.setForumCommentCount(0);
            stats.setForumFollowingCount(0);
            stats.setForumFollowerCount(0);
            return forumUserStatsMapper.insert(stats) > 0;
        }
        
        return true;
    }

    @Override
    public ForumUserStatsDTO getUserStats(Integer userId) {
        // 確保統計記錄存在
        updateUserStats(userId);
        
        ForumUserStats stats = forumUserStatsMapper.selectById(userId);
        if (stats == null) {
            return null;
        }
        
        return convertToDTO(stats);
    }

    @Override
    public boolean incrementPostCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_post_count = forum_post_count + 1");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean decrementPostCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_post_count = GREATEST(forum_post_count - 1, 0)");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean incrementLikeCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_like_count = forum_like_count + 1");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean decrementLikeCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_like_count = GREATEST(forum_like_count - 1, 0)");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean incrementCommentCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_comment_count = forum_comment_count + 1");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean decrementCommentCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_comment_count = GREATEST(forum_comment_count - 1, 0)");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean incrementFollowingCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_following_count = forum_following_count + 1");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean decrementFollowingCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_following_count = GREATEST(forum_following_count - 1, 0)");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean incrementFollowerCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_follower_count = forum_follower_count + 1");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean decrementFollowerCount(Integer userId) {
        UpdateWrapper<ForumUserStats> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                    .setSql("forum_follower_count = GREATEST(forum_follower_count - 1, 0)");
        
        return forumUserStatsMapper.update(null, updateWrapper) > 0;
    }

    /**
     * 轉換為 DTO
     */
    private ForumUserStatsDTO convertToDTO(ForumUserStats stats) {
        ForumUserStatsDTO dto = new ForumUserStatsDTO();
        BeanUtils.copyProperties(stats, dto);
        
        // 獲取用戶信息
        User user = userMapper.selectById(stats.getUserId());
        if (user != null) {
            dto.setUsername(user.getUsername());
            dto.setAvatar(user.getAvatar());
        }
        
        return dto;
    }
} 